<!DOCTYPE html>
<html>
<head>
    <title>栏目管理</title>
    {% include 'system/common/header.html' %}
</head>
<body>
<form class="layui-form" action="">
    <div class="mainBox">
        <div class="main-container">
            <div class="main-container">
                <input type="hidden" value="{{ cate.id }}" name="categoryId" >
				<div class="layui-form-item">
				    <label class="layui-form-label">上级栏目</label>
				    <div class="layui-input-block">
				        <ul id="selectParent" name="parentId" class="dtree" data-id="-1"></ul>
				    </div>
				</div>
                <div class="layui-form-item">
                    <label class="layui-form-label">栏目名称</label>
                    <div class="layui-input-block">
                        <input type="text" value="{{ cate.category_name }}" name="categoryName" lay-verify="title"
                               autocomplete="off" placeholder="请输入栏目名称" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">跳转地址</label>
                    <div class="layui-input-block">
                        <input type="text" value="{{ cate.category_url|default('', true) }}" name="categoryUrl" lay-verify="title" autocomplete="off"
                               placeholder="请输入跳转地址"
                               class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">状态</label>
                    <div class="layui-input-block">

                        <input type="radio" name="status" {% if cate.status == 1 %}checked{% endif %} value="1"
                               title="开启">

                        <input type="radio" name="status" {% if cate.status == 0 %}checked{% endif %} value="0"
                               title="关闭"
                        >
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">排序</label>
                    <div class="layui-input-block">
                        <input type="text" name="sort" value="{{ cate.sort }}" lay-verify="title" autocomplete="off"
                               placeholder="请输入排序" value="0"
                               class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-form-text">
                    <label class="layui-form-label">备注</label>
                    <div class="layui-input-block">
                        <textarea placeholder="请输入备注" name="remark"
                                  class="layui-textarea">{{ cate.remark|default('', true) }}</textarea>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit=""
                    lay-filter="cate-update">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="pear-btn pear-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
{% include 'system/common/footer.html' %}
<script>
  layui.use(['form', 'jquery', 'dtree'], function () {
    let form = layui.form
    let $ = layui.jquery
    let dtree = layui.dtree

    dtree.renderSelect({
      elem: '#selectParent',
      url: '/cms/category/tree',
      method: 'get',
      selectInputName: { nodeId: 'parentId', context: 'parentName' },
      skin: 'layui',
      dataFormat: 'list',
      response: { treeId: 'id', parentId: 'parent_id', title: 'category_name' },
      selectInitVal: '{{ cate.parent_id }}'
    })
	
	//绑定点击节点
	dtree.on("node('selectParent')",function(obj){
		 console.log(123,dtree);
		 // 获取当前点击的节点
		 var nodeId = '{{ cate.id }}';
		 // 获取当前点击节点的父节点ID
		 var parentId = obj.param.nodeId;
		 	  
		 // 如果点击的节点和它的父节点相同，即选择了自身
		 if (nodeId === parentId) {
		   // 取消选择
		   dtree.selectVal('{{ cate.parent_id }}');
		   // 提示用户
		   layer.msg('不能选择自身节点');
		 }
	})
	

    form.on('submit(cate-update)', function (data) {
      $.ajax({
        url: '/cms/category/update',
        data: JSON.stringify(data.field),
        dataType: 'json',
        contentType: 'application/json',
        type: 'put',
        success: function (result) {
          if (result.success) {
            layer.msg(result.msg, { icon: 1, time: 1000 }, function () {
              parent.layer.close(parent.layer.getFrameIndex(window.name))//关闭当前页
              parent.render()
            })
          } else {
            layer.msg(result.msg, { icon: 2, time: 1000 })
          }
        }
      })
      return false
    })
  })
</script>
<script>
</script>
</body>
</html>